package com.wangzeqi.changliaoba.singleton;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.wangzeqi.changliaoba.R;
import com.wangzeqi.changliaoba.base.BaseApplication;

/**
 * Created by dllo on 16/3/4.
 */
public class QueueSingleTon {

    private volatile static QueueSingleTon instance;
    private RequestQueue queue;
    private ImageLoader imageLoader;
    private DoubleCache doubleCache;


    public static QueueSingleTon getInstance() {
        if (instance == null) {
            synchronized (QueueSingleTon.class) {
                if (instance == null) {
                    instance = new QueueSingleTon();
                }
            }
        }
        return instance;
    }

    private QueueSingleTon() {
        queue = getQueue();
        doubleCache = getCache();
        imageLoader = getImageLoader();
    }

    public RequestQueue getQueue() {
        if (queue == null) {
            queue = Volley.newRequestQueue(BaseApplication.applicationContext);
        }
        return queue;
    }

    public DoubleCache getCache() {
        if (doubleCache == null) {
            doubleCache = new DoubleCache();
        }
        return doubleCache;
    }

    public ImageLoader getImageLoader() {
        if (imageLoader == null) {
            imageLoader = new ImageLoader(getQueue(), doubleCache);
        }

        return imageLoader;
    }

    public void addRequest(StringRequest request) {
        getInstance().getQueue().add(request);
    }

    public void imgJson(NetworkImageView networkImageView, String url) {
        networkImageView.setDefaultImageResId(R.mipmap.iconfont_jiazaishibainew);
        networkImageView.setErrorImageResId(R.mipmap.ic_launcher);
        networkImageView.setImageUrl(url, imageLoader);
    }


}
